package org.eclipsercp.hyperbola.model;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class AddContactDialog extends TitleAreaDialog {
	private Text txtName;
	private Text txtNickname;
	private Text txtServer;
	private ContactEntry entry;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 */
	public AddContactDialog(Shell parentShell) {
		super(parentShell);
		Object data = parentShell.getData();
		if (data instanceof ContactEntry) {
			entry = (ContactEntry) data;
		} else {
			entry = new ContactEntry();
		}
	}

	public ContactEntry getEntry() {
		return entry;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		setMessage("搜索");
		setTitle("添加联系人");
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		container.setLayoutData(new GridData(GridData.FILL_BOTH));

		Label lblName = new Label(container, SWT.NONE);
		lblName.setBounds(86, 23, 36, 17);
		lblName.setText("姓名：");

		txtName = new Text(container, SWT.BORDER);
		txtName.setBounds(128, 18, 202, 23);

		Label lblNickname = new Label(container, SWT.NONE);
		lblNickname.setText("昵称：");
		lblNickname.setBounds(86, 63, 36, 17);

		txtNickname = new Text(container, SWT.BORDER);
		txtNickname.setBounds(128, 59, 202, 23);

		Label lblServer = new Label(container, SWT.NONE);
		lblServer.setText("地址：");
		lblServer.setBounds(86, 103, 36, 17);

		txtServer = new Text(container, SWT.BORDER);
		txtServer.setBounds(128, 100, 202, 23);

		return area;
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		Button button = createButton(parent, IDialogConstants.OK_ID,
				IDialogConstants.OK_LABEL, true);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
			}
		});
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
		initDataBindings();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue observeTextTxtNameObserveWidget = WidgetProperties
				.text(SWT.FocusOut).observe(txtName);
		IObservableValue nameEntryObserveValue = PojoProperties.value("name")
				.observe(entry);
		bindingContext.bindValue(observeTextTxtNameObserveWidget,
				nameEntryObserveValue, null, null);
		//
		IObservableValue observeTextTxtNicknameObserveWidget = WidgetProperties
				.text(SWT.FocusOut).observe(txtNickname);
		IObservableValue nicknameEntryObserveValue = PojoProperties.value(
				"nickname").observe(entry);
		bindingContext.bindValue(observeTextTxtNicknameObserveWidget,
				nicknameEntryObserveValue, null, null);
		//
		IObservableValue observeTextTxtServerObserveWidget = WidgetProperties
				.text(SWT.FocusOut).observe(txtServer);
		IObservableValue serverEntryObserveValue = PojoProperties.value(
				"server").observe(entry);
		bindingContext.bindValue(observeTextTxtServerObserveWidget,
				serverEntryObserveValue, null, null);
		//
		return bindingContext;
	}
}
